package com.android.tiango.baba.act;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.Selection;
import android.text.Spannable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.util.CommUtil;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.ToastUtils;

/**
 * 找回密码
 * 
 * @author suip
 * 
 */
public class ActFindPassword extends BaBaseActivity implements OnClickListener {

	private EditText mPhone;
	private EditText mPassword;// login 的手机号码账号和密码
	private Button mBtCode;
	private EditText mEtCode;
	private ImageView mIsOpenPassword;// 控制密码是否显示为***
	private TextView mTvCode;// 验证码重发时间
	private Button mFinish;
	private ImageView mUserCode;// 是否阅读并遵守用户守则
	private ImageView mDelete;
	private boolean mIscheck = false;
	private boolean isCodeTime = false;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.layout_find_password);
		initView();

	}

	private void initView() {
		// TODO Auto-generated method stub
		setTitleName("找回密码");
		if (null != mTitleBar) {
			mTitleBar.setBackIcon(R.drawable.back);
			setBackOnClickListener();
		}
		mPhone = (EditText) findViewById(R.id.ic_enter).findViewById(
				R.id.et_phone);
		mPassword = (EditText) findViewById(R.id.ic_enter).findViewById(
				R.id.et_password);
		mBtCode = (Button) findViewById(R.id.bt_code);
		mEtCode = (EditText) findViewById(R.id.et_code);
		mIsOpenPassword = (ImageView) findViewById(R.id.ic_enter).findViewById(
				R.id.iv_passwordclose);
		mFinish = (Button) findViewById(R.id.bt_login);
		mTvCode = (TextView) findViewById(R.id.tv_code);

		mIsOpenPassword.setImageResource(R.drawable.password_open2);
		mPhone.setHint("手机号");
		mPhone.setHintTextColor(Color.GRAY);
		mPassword.setHintTextColor(Color.GRAY);
		//mPhone.setInputType(EditorInfo.TYPE_CLASS_PHONE);// 弹出数字输入框
		mEtCode.setInputType(EditorInfo.TYPE_CLASS_PHONE);// 弹出数字输入框
		mPhone.setTextColor(Color.BLACK);
		mPassword.setTextColor(Color.BLACK);
		mIsOpenPassword.setOnClickListener(this);
		mBtCode.setOnClickListener(this);
		mFinish.setOnClickListener(this);
		
		String phoneNumber=ShareCookie.getUserAccount();
		mPhone.setText(phoneNumber);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_passwordclose:
			if (mIscheck == false) {
				mIsOpenPassword.setImageResource(R.drawable.password_open2);
				mPassword
						.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				setCursor(mPassword);
				mIscheck = true;
			} else {
				mIsOpenPassword.setImageResource(R.drawable.password_close2);
				mPassword.setInputType(InputType.TYPE_CLASS_TEXT
						| InputType.TYPE_TEXT_VARIATION_PASSWORD);
				setCursor(mPassword);
				mIscheck = false;
			}
			break;
		case R.id.bt_code:
			if (CommUtil.isMobileNO(mPhone.getText().toString())) {
				if (isCodeTime == false) {
					onLoadCode();
				} else {
					ToastUtils.showMessage("请稍后点击");
				}
				showWaitingDialog();
			} else {
				ToastUtils.showMessage("请输入正确手机号码");
			}
			break;
		case R.id.bt_login:
			if (CommUtil.isNetworkAvailable(this)) {
				String phone = mPhone.getText().toString().trim();
				String pass = mPassword.getText().toString().trim();
				String code=mEtCode.getText().toString().trim();
				if(StringUtils.isEmpty(phone)){
					ToastUtils.showMessage("手机号码不能为空！！");
					return;
				}
				if(StringUtils.isEmpty(pass)){
					ToastUtils.showMessage("密码不能为空！！");
					return;
				}
				if(StringUtils.isEmpty(code)){
					ToastUtils.showMessage("验证码不能为空！！");
					return;
				}
				if(CommUtil.isMobileNO(phone)){
					showWaitingDialog();
					onLoadDate();
				}else{
					ToastUtils.showMessage("请输入正确手机号码");
					return;
				}
				
			} else {
				ToastUtils.showMessage("没有网络连接");
			}
			break;
		default:
			break;
		}
	}

	private void onLoadDate() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams httpparams = new HttpParams();
		httpparams.put("userName", mPhone.getText().toString().trim());
		httpparams.put("newPassword", mPassword.getText().toString().trim());
		httpparams.put("captcha", mEtCode.getText().toString().trim());
		client.post(HttpUrl.getUrl(HttpUrl.FINDPASSWORD), httpparams,
				new HttpCallBack() {

					@Override
					public void onHttpSuccess(Object arg0) {
						// TODO Auto-generated method stub
						ToastUtils.showMessage("成功！！");
						dismissWaitingDialog();
						showActivity(ActHome.class, true);
					}

					@Override
					public void onHttpStarted() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onHttpFailure(Exception arg0, String arg1) {
						// TODO Auto-generated method stub
						dismissWaitingDialog();
						ToastUtils.showMessage("失败！===" + arg1);
						arg0.printStackTrace();
					}
				});
	}

	private void onLoadCode() {
		// TODO Auto-generated method stub
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams httpparams = new HttpParams();
		httpparams.put("userName", mPhone.getText().toString());
		httpparams.put("sendType", "1");

		client.post(HttpUrl.getUrl(HttpUrl.CODE), httpparams,
				new HttpCallBack() {

					@Override
					public void onHttpSuccess(Object arg0) {
						// TODO Auto-generated method stub
						ToastUtils.showMessage("验证码获取成功！");
						dismissWaitingDialog();
						final long time1 = System.currentTimeMillis();
						isCodeTime = true;
						new Thread(new Runnable() {

							@Override
							public void run() {
								// TODO Auto-generated method stub
								while (isCodeTime) {
									try {
										Thread.sleep(1000);
										long time2 = System.currentTimeMillis();

										Message message = new Message();
										message.what = 0204;
										message.arg1 = (int) (time2 - time1);
										handler.sendMessage(message);

									} catch (InterruptedException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							}
						}).start();
					}

					@Override
					public void onHttpStarted() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onHttpFailure(Exception arg0, String arg1) {
						// TODO Auto-generated method stub
						dismissWaitingDialog();
						ToastUtils.showMessage("验证码获取失败！===" + arg1);

						arg0.printStackTrace();
					}
				});
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0204:
				if (60 - (msg.arg1 / 1000) < 0) {
					isCodeTime = false;
					mBtCode.setTextColor(Color.WHITE);
				} else {
					mTvCode.setText(60 - (int) (msg.arg1 / 1000) + "");
					mBtCode.setTextColor(Color.GRAY);
				}

				break;

			default:
				break;
			}
		};
	};

	public void setCursor(EditText editText) {
		CharSequence text = editText.getText();
		// Debug.asserts(text instanceof Spannable);
		if (text instanceof Spannable) {
			Spannable spanText = (Spannable) text;
			Selection.setSelection(spanText, text.length());
		}
	}
}
